#include <libwnck/libwnck.h>
#include <gdk/gdkkeysyms.h>

#include "launchers_pad.h"
#include "launcher.h"
#include "wspace_switcher.h"
#include "globals.h"
#include "config.h"


GtkWidget *launchers[MAX_LAUNCHERS]; // FIXME 16x16 maximum


/**
 * launchers_pad_new:
 *
 * Create new GtkTable widget
 * and create and attach launchers to it.
 *
 * Return value: a new GtkWidget.
 **/
GtkWidget * launchers_pad_new (GtkWidget * window)
{
    GtkWidget * launchers_table;
    GtkWidget * button;
    guint       x, y;
    guint       i;
    GtkAccelGroup * accel_group;


    /*
        GtkAccelGroup *accel_group;
        accel_group=gtk_accel_group_new();
        gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
    */

    // table - launchers buttons
    launchers_table = gtk_table_new (config.rows, config.cols, TRUE);

    accel_group = gtk_accel_group_new();
    gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);

    i = 1;
    for (y = 0; y < config.rows; y++)
        for (x = 0; x < config.cols; x++)
        {
            //button = launcher_new (config.rows*y + (x+1));
            button = launcher_new (i);

            // hotkeys
            launchers_pad_set_hotkey(button, accel_group, i);

            // save launcher to array
            launchers[config.rows*y + x] = button;

            gtk_table_attach (GTK_TABLE(launchers_table), button,
                              x, x+1, y, y+1,
                              GTK_EXPAND | GTK_FILL,
                              GTK_EXPAND | GTK_FILL,
                              2, 2);
            i++;
        }

    // load icons and launcher names
    //launchers_pad_load_launchers ();

    //wnck_screen_get_workspace_index(screen, workspace);
    // toggle wspace radio button

    //"active-workspace-changed"
    g_signal_connect (G_OBJECT (screen), "active-workspace-changed",
                      G_CALLBACK (wspace_switcher_on_change_wspace), NULL);

    return launchers_table;
}


/**
 * launchers_pad_load_launchers:
 *   @wspace_name: name of workspace. Current workspace name.
 *
 * For given workspace load icons from .desktop-files and decorate launchers
 **/
void launchers_pad_load_launchers ()
{
    GtkWidget *     button = NULL;
    gchar *         filename;
    guint           id;
    gpointer        id_ptr;
    gchar           id_str[STR_ID_LEN+1];
    guint           x, y;
    const gchar *   wspace_name;
    WnckWorkspace * workspace;


    workspace =  wnck_screen_get_active_workspace (screen);

    // Try detect number if active worckspace not detected (compiz+wnck bug)
    if (workspace != NULL)
    {
        wspace_name = wnck_workspace_get_name (workspace);
    }
    else
    {
        wspace_name = wnck_workspace_get_name (0);
    }

    // try detect name (compiz+wnck bug)
    if (strlen(wspace_name) == 0)
    {
        wspace_name = "Main";
    }

    //g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "%s", wspace_name);


    for (y=0; y < config.rows; y++)
        for (x=0; x < config.cols; x++)
        {
            button = launchers[config.rows*y + x];

            // get id
            id_ptr = g_object_get_data (G_OBJECT(button), "id");
            id = GPOINTER_TO_UINT (id_ptr);
            g_snprintf(id_str, STR_ID_LEN, "%d", id);

            // path to .desktop file
            filename = g_strjoin(NULL, config.dir, "/", wspace_name, "/", id_str, ".desktop", NULL);

            launcher_decorate(button, filename);

            g_free(filename);
        }
}


/**
 * launchers_pad_get_desktop_app_icon:
 *   @appinfo:  GDesktopAppInfo *
 *
 * Read icon name from .desktop file structure.
 * Create new GtkImage widget.
 *
 * Return value: a new GtkWidget.
 **/
GtkWidget * launchers_pad_get_desktop_app_icon (GDesktopAppInfo *appinfo)
{
    GIcon *     icon;
    GtkWidget * icon_widget = NULL;


    if (appinfo)
    {
        icon = g_app_info_get_icon (G_APP_INFO(appinfo));
        if (icon)
        {
            icon_widget = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_DIALOG);
        }
        else
            g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "%s", "Can't load icon.");
    }
    else
    {
        // FIXME set any default icon
        g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "%s", "Can't load application icon. Setting default icon.");
    }

    return icon_widget; // GtkImage
}


/**
 * set_hotkey:
 *   @button: GtkWidget *
 *   @accel_group: GtkAccelGroup *
 *   @index: launcher number (id)
 *
 * Set hotkey to launcher by him id.
 * Hotkey is a number button on main keyboard and keypad.
 **/
 void launchers_pad_set_hotkey(GtkWidget * button, GtkAccelGroup * accel_group, guint index)
{
    guint hkeys[][2] = {
        {GDK_0, GDK_KP_0}, // not used
        {GDK_1, GDK_KP_1},
        {GDK_2, GDK_KP_2},
        {GDK_3, GDK_KP_3},
        {GDK_4, GDK_KP_4},
        {GDK_5, GDK_KP_5},
        {GDK_6, GDK_KP_6},
        {GDK_7, GDK_KP_7},
        {GDK_8, GDK_KP_8},
        {GDK_9, GDK_KP_9}
    };

    gtk_widget_add_accelerator(button, "clicked",
                               accel_group,
                               hkeys[index][0],
                               0, 0);

    gtk_widget_add_accelerator(button, "clicked",
                               accel_group,
                               hkeys[index][1],
                               0, 0);
}
